﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Common;

namespace Catering.UserTraining.SlrCompanyTrainning
{
    public partial class cpntraining_test_que_add : UserIsNull
    {
        public string m_UserID = "0";
        public int m_TestID = 0;
        public int m_LessonID = 0;
        public int m_QueID = 0;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Session["UserID"] != null) m_UserID = Session["UserID"].ToString();
            m_TestID = CommRequest.GetInt("TestID", 0);
            m_LessonID = CommRequest.GetInt("LessonID", 0);
            m_QueID = CommRequest.GetInt("QueID", 0);
            if (!IsPostBack)
            {
                this.lbl_TestName.Text = "新增题库项";
                //增加或者修改判断。
                if (!string.IsNullOrEmpty(CommRequest.GetString("Flag")))
                {
                    this.hid_Flag.Value = CommRequest.GetString("Flag");
                }

                if (this.hid_Flag.Value == "EDIT")
                {
                    this.InitQueInfo();
                }
            }
        }

        private void InitQueInfo()
        {
            this.lbl_TestName.Text = new Catering_Rules.Tests().GetModel(m_TestID).TestName;
            Catering_Model.TestsQueList _QueModel = new Catering_Model.TestsQueList();
            _QueModel = new Catering_Rules.TestsQueList().GetModel(m_QueID);
            if (_QueModel == null) return;
            ViewState["EditTestsQueListodel"] = _QueModel;
            this.txt_QueName.Text = _QueModel.QueName;
            this.txt_ItemA.Text = _QueModel.QueContentA;
            this.txt_ItemB.Text = _QueModel.QueContentB;
            this.txt_ItemC.Text = _QueModel.QueContentC;
            this.txt_ItemD.Text = _QueModel.QueContentD;
            this.txt_QueNo.Text = _QueModel.QueNO;
            this.cb_ItemA.Checked = false;
            this.cb_ItemB.Checked = false;
            this.cb_ItemC.Checked = false;
            this.cb_ItemD.Checked = false;
            foreach (string strtemp in CommUtils.SplitString(_QueModel.QueAnswer.TrimEnd('|'), "|"))
            {
                switch (strtemp)
                {
                    case "A":
                        this.cb_ItemA.Checked = true;
                        break;
                    case "B":
                        this.cb_ItemB.Checked = true;
                        break;
                    case "C":
                        this.cb_ItemC.Checked = true;
                        break;
                    case "D":
                        this.cb_ItemD.Checked = true;
                        break;
                }
            }
        }

        /// <summary>
        /// 添加或者修改
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnAdd_Click(object sender, EventArgs e)
        {
            string sMsg = "";
            Catering_Model.TestsQueList _TestsQueListModel = new Catering_Model.TestsQueList();
            _TestsQueListModel.QueName = this.txt_QueName.Text.Trim();
            _TestsQueListModel.QueContentA = this.txt_ItemA.Text.Trim();
            _TestsQueListModel.QueContentB = this.txt_ItemB.Text.Trim();
            _TestsQueListModel.QueContentC = this.txt_ItemC.Text.Trim();
            _TestsQueListModel.QueContentD = this.txt_ItemD.Text.Trim();
            _TestsQueListModel.QueNO = this.txt_QueNo.Text.Trim();
            _TestsQueListModel.QueAnswer = "";
            _TestsQueListModel.QueAnswer = this.cb_ItemA.Checked ? _TestsQueListModel.QueAnswer + "A|" : _TestsQueListModel.QueAnswer;
            _TestsQueListModel.QueAnswer = this.cb_ItemB.Checked ? _TestsQueListModel.QueAnswer + "B|" : _TestsQueListModel.QueAnswer;
            _TestsQueListModel.QueAnswer = this.cb_ItemC.Checked ? _TestsQueListModel.QueAnswer + "C|" : _TestsQueListModel.QueAnswer;
            _TestsQueListModel.QueAnswer = this.cb_ItemD.Checked ? _TestsQueListModel.QueAnswer + "D|" : _TestsQueListModel.QueAnswer;
            _TestsQueListModel.TestID = m_TestID;
            _TestsQueListModel.QueType = _TestsQueListModel.QueAnswer.Length == 2 ? "S" : "M";
            if (this.hid_Flag.Value == "EDIT")
            {
                Catering_Model.TestsQueList _OldTestsQueListModel = (Catering_Model.TestsQueList)ViewState["EditTestsQueListodel"];
                _TestsQueListModel.QueID = m_QueID;
                //_TestsQueListModel.QueNO = _OldTestsQueListModel.QueNO;
                _TestsQueListModel.AddUserID = _OldTestsQueListModel.AddUserID;
                _TestsQueListModel.AddTime = _OldTestsQueListModel.AddTime;
                new Catering_Rules.TestsQueList().CheckTestsQueListModel(_TestsQueListModel, out sMsg);
                if (string.IsNullOrEmpty(sMsg) && new Catering_Rules.TestsQueList().Update(_TestsQueListModel))
                {
                    RegisterStartupScript("保存成功!");
                }
                else
                {
                    RegisterStartupScript("保存失败！" + sMsg);
                }
            }
            else
            {
                _TestsQueListModel.QueID = m_QueID;
                //_TestsQueListModel.QueNO = "";
                _TestsQueListModel.AddUserID = int.Parse(Session["UserID"].ToString());
                _TestsQueListModel.AddTime = DateTime.Now;
                new Catering_Rules.TestsQueList().CheckTestsQueListModel(_TestsQueListModel, out sMsg);

                if (string.IsNullOrEmpty(sMsg))
                {
                    int intNewTestID = new Catering_Rules.TestsQueList().Add(_TestsQueListModel);
                    if (intNewTestID > 0)
                    {
                        RegisterStartupScript("保存成功!");
                    }
                    else
                    {
                        RegisterStartupScript("保存失败！" + sMsg);
                    }
                }
                else
                {
                    RegisterStartupScript("保存失败！" + sMsg);
                }
            }
        }

        public void RegisterStartupScript(string strMessage)
        {
            ScriptManager.RegisterStartupScript(this, GetType(), "Affect", "alert('" + strMessage + "！')", true);
        }

        protected void btn_Return_Click(object sender, EventArgs e)
        {
            string url = "~/UserTraining/SlrCompanyTrainning/cpntraining_test_que_list.aspx?LessonID=" + m_LessonID + "&TestID=" + m_TestID;
            Response.Redirect(url);
        }
    }
}